package org.kms.Controllers;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.kms.dao.fxinfoDao;
import org.kms.pojo.*;
import org.kms.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/Fxinfo")
@CrossOrigin(value = "*")
public class fxinfo {

    @Autowired
    private fxinfoDao fxinfoDao;

    @RequestMapping("/all")
    @ResponseBody
    public ResponseResult<List<Fxinfo>> list(@RequestParam(defaultValue = "1") int page, String cha, @RequestParam(defaultValue = "10") int limit, Fxinfo condition){
        if(cha!=null&&!cha.equals("")){
            Fxtype f = new Fxtype();
            f.setTypesid(Integer.parseInt(cha));
            condition.setFxtype(f);
        }
        Page<Fxinfo> pager = PageHelper.startPage(page,limit);
        List<Fxinfo> list = fxinfoDao.getConditionList(condition);
        return new ResponseResult(list, pager.getTotal());
    }

    @RequestMapping("/delete")
    @ResponseBody
    public ResponseResult delete(Integer id) throws Exception{
        fxinfoDao.delete(id);
        return ResponseResult.createSuccessResult();
    }

    @RequestMapping("/batchDelete")
    @ResponseBody
    public ResponseResult batchDelete(Integer[] id)throws Exception{
        fxinfoDao.batchDelete(id);
        return ResponseResult.createSuccessResult();
    }

    @InitBinder
    public void InitBinder(WebDataBinder webDataBinder) throws Exception{
        //日期类型转换器
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        CustomDateEditor dateEditor = new CustomDateEditor(simpleDateFormat,true);
        //注册类型换换器:如果碰到Date.class 都会转成yyyy-MM-dd格式
        webDataBinder.registerCustomEditor(Date.class,dateEditor);
    }


    @RequestMapping("/add")
    @ResponseBody
    public ResponseResult add(Fxinfo fxinfo,String roleIds)throws Exception{
        fxinfoDao.add(fxinfo);
        return ResponseResult.createSuccessResult();
    }


    @RequestMapping("/update")
    @ResponseBody
    private ResponseResult update(Fxinfo fxinfo,String roleIds)throws Exception{
        fxinfoDao.update(fxinfo);
        return ResponseResult.createSuccessResult();
    }

    @RequestMapping("/fxall")
    @ResponseBody
    public ResponseResult all(Integer glId)throws Exception{
        List<Fxinfo> fxinfos = fxinfoDao.getbyfxinfos(glId);
        return new ResponseResult(fxinfos);
    }

}
